package com.wuqi.dev.retrofitdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wuqi.dev.retrofitdemo.entity.BrandEntity;
import com.wuqi.dev.retrofitdemo.entity.Temp;
import com.wuqi.dev.retrofitdemo.retrofit.BearService;
import com.wuqi.dev.retrofitdemo.retrofit.HttpManger;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        Type type = new TypeToken<List<BrandEntity>>(){}.getType();
//        Gson gson = new Gson();
//        List<BrandEntity> list = gson.fromJson("...", type);


        // Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.xiaoxiongyouhao.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        BearService service = retrofit.create(BearService.class);
//        Call<BrandEntity> brands = service.getBrands();
//        brands.enqueue(new Callback<BrandEntity>() {
//            @Override
//            public void onResponse (Call<BrandEntity> call, Response<BrandEntity> response) {
//                BrandEntity body = response.body();
//
//            }
//
//            @Override
//            public void onFailure (Call<BrandEntity> call, Throwable t) {
//
//            }
//        });

        // 根据不同的index获取车系
        Call<BrandEntity> carSeries = service.getCarSeries(33);
        carSeries.enqueue(new Callback<BrandEntity>() {
            @Override
            public void onResponse (Call<BrandEntity> call, Response<BrandEntity> response) {

            }

            @Override
            public void onFailure (Call<BrandEntity> call, Throwable t) {

            }
        });

        // 同步请求方式
//        Call<BrandEntity> brands = HttpManger.getInstance().getService().getBrands();
//        try {
//            Response<BrandEntity> execute = brands.execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        HttpManger.getInstance().getApi()
                .getBrands()
                .flatMap(new Function<BrandEntity, ObservableSource<Temp>>() {
                    @Override
                    public ObservableSource<Temp> apply (BrandEntity brandEntity) throws Exception {
                        List<Integer> indexs = brandEntity.getIdxes();
                        List<String> names = brandEntity.getNames();
                        return Observable.zip(Observable.fromIterable(indexs), Observable.fromIterable(names), new BiFunction<Integer, String, Temp>() {
                            @Override
                            public Temp apply (Integer integer, String s) throws Exception {
                                Temp t = new Temp();
                                t.setIndex(integer);
                                t.setName(s);
                                return t;
                            }
                        });
                    }
                })
                .toList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<Temp>>() {
                    @Override
                    public void accept (List<Temp> temps) throws Exception {

                    }
                });


    }
}
